package com.itheima.day20.demo06;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.TreeSet;

/**
 * 5. 已知项目下有个a.txt文本文件，里边有两行数据，如下：
 * a,c,d,e,g,h
 * a,g,b,o,q,r
 * 需求：
 * 创建输入流，读取这两行数据，每行数据都可以看做是一个ArrayList<String>集合，而每行的字母就是该集合中的元素。
 * 对两个集合中的元素去重后合并，然后将去重后的结果写入到项目下的b.txt文本文件中。
 */
public class demo05_02 {
    public static final String SRC = "./homework/src/com/itheima/day20/demo06/a.txt";
    public static final String DEST = "./homework/src/com/itheima/day20/demo06/b.txt";

    public static void main(String[] args) throws Exception {
        TreeSet<String> ts = new TreeSet<>();
        BufferedReader br = new BufferedReader(new FileReader(new File(SRC)));
        BufferedWriter bw = new BufferedWriter(new BufferedWriter(new FileWriter(DEST)));
        String s = null;
        while ((s = br.readLine()) != null) {
            ArrayList<String> list = new ArrayList<>();
            list.addAll(Arrays.asList(s.split(",")));
            ts.addAll(list);
        }
        ArrayList<String> list = new ArrayList<>();
        list.addAll(ts);
        for (int i = 0; i < list.size(); i++) {
            String str = i == list.size() - 1 ? list.get(i) : list.get(i) + ",";
            bw.write(str);
        }
        bw.close();
        br.close();
    }
}
